package cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail.vo.ShipOrderDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail.vo.ShipOrderDetailRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail.vo.ShipOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.materialmode.MaterialModeDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.shiporder.ShipOrderDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.shiporderdetail.ShipOrderDetailDO;
import cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.hs.service.materialmode.MaterialModeService;
import cn.iocoder.yudao.module.hs.service.shiporder.ShipOrderService;
import cn.iocoder.yudao.module.hs.service.shiporderdetail.ShipOrderDetailService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 出库单明细")
@RestController
@RequestMapping("/hs/ship-order-detail")
@Validated
public class ShipOrderDetailController {

    @Resource
    private ShipOrderDetailService shipOrderDetailService;
    @Resource
    private ShipOrderService shipOrderService;
    @Resource
    private MaterialModeService materialModeService;
    @Autowired
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建出库单明细")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:create')")
    public CommonResult<Long> createShipOrderDetail(@Valid @RequestBody ShipOrderDetailSaveReqVO createReqVO) {
        return success(shipOrderDetailService.createShipOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出库单明细")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:update')")
    public CommonResult<Boolean> updateShipOrderDetail(@Valid @RequestBody ShipOrderDetailSaveReqVO updateReqVO) {
        shipOrderDetailService.updateShipOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出库单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:delete')")
    public CommonResult<Boolean> deleteShipOrderDetail(@RequestParam("id") Long id) {
        shipOrderDetailService.deleteShipOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出库单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:query')")
    public CommonResult<ShipOrderDetailRespVO> getShipOrderDetail(@RequestParam("id") Long id) {
        ShipOrderDetailDO shipOrderDetail = shipOrderDetailService.getShipOrderDetail(id);
        return success(BeanUtils.toBean(shipOrderDetail, ShipOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出库单明细分页")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:query')")
    public CommonResult<PageResult<ShipOrderDetailRespVO>> getShipOrderDetailPage(@Valid ShipOrderDetailPageReqVO pageReqVO) {
        PageResult<ShipOrderDetailDO> pageResult = shipOrderDetailService.getShipOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShipOrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出库单明细 Excel")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShipOrderDetailExcel(@Valid ShipOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShipOrderDetailDO> list = shipOrderDetailService.getShipOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出库单明细.xls", "数据", ShipOrderDetailRespVO.class,
                        BeanUtils.toBean(list, ShipOrderDetailRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得出库单明细list")
    @PreAuthorize("@ss.hasPermission('hs:ship-order-detail:query')")
    public CommonResult<List<ShipOrderDetailRespVO>> getShipOrderDetailList(@RequestParam("shipOrderCode") String shipOrderCode) {
        ShipOrderDO shipOrder = shipOrderService.getShipOrder(shipOrderCode);
        if (shipOrder == null) {
            return error(ErrorCodeConstants.SHIP_ORDER_NOT_EXISTS);
        }
        List<ShipOrderDetailDO> list = shipOrderDetailService.getShipOrderDetailList(shipOrder.getId());
        List<ShipOrderDetailRespVO> bean = BeanUtils.toBean(list, ShipOrderDetailRespVO.class);
        for (ShipOrderDetailRespVO shipOrderDetailRespVO : bean) {
            MaterialModeDO materialMode = materialModeService.getMaterialMode(shipOrderDetailRespVO.getModeCode(), shipOrder.getIsDirect());
            if (materialMode != null) {
                shipOrderDetailRespVO.setPartNumber(materialMode.getPartNumber());
                shipOrderDetailRespVO.setPartNumberShort(materialMode.getPartNumberShort());
            } else {
                shipOrderDetailRespVO.setPartNumber("/");
                shipOrderDetailRespVO.setPartNumberShort("/");
            }
        }
        return success(bean);
    }
}